<?php

namespace app\common\service\bangdou;

use app\api\service\User as UserService;
use app\common\model\UserOauth;
use app\common\service\BaseService;
use app\common\service\bangdou\JwtClass as JwtClassService;

class Common extends BaseService
{
    const SITESECRET = 'KJMLIOUJKIUYTTTDDTTUUUEEEbq5xbsqLV3D948Sbgq2ii0llLOLQaiK0Imondu09';
    const ORGANID = '7777';

    public static function getBangDouApiSign($allParams)
    {
        ksort($allParams);
        $signContent = '';
        foreach ($allParams as $key => $value) {
            if (empty($value)) {
                continue;
            }
            if (is_array($value)) {
                $value = json_encode($value);
            }

            $signContent .= $key . '=' . $value . '&';
        }
        $sign = strtoupper(sha1($signContent . self::SITESECRET));
        return $sign;
    }

    /**
     * 通过邦豆平台用户id创建token创建token
     */
    public static function createToken($bd_uid)
    {
        //jwt过期时间
        $expireTime = time() + (7 * 24 * 3600);
        $payload = array(
            'sub' => $bd_uid,
            'exp' => $expireTime,
        );
        $encoded = JwtClassService::encode($payload, self::SITESECRET);
        return [
            'expireTime' => $expireTime,
            'token' => $encoded,
        ];
    }

    /**
     * 通过平台用户id创建token
     */
    public static function createTokenByuserId($userId=0)
    {
        $bd_uid = '';
        if($userId){
            $userInfo = UserOauth::getOauth($userId,'MP-WEIXIN');
            if (isset($userInfo['bd_uid']) && !empty($userInfo['bd_uid'])) {
                $bd_uid = $userInfo['bd_uid'];
            }
        }else{
            $userInfo = UserService::getCurrentLoginUser(true);
            if (isset($userInfo['currentOauth']['bd_uid']) && !empty($userInfo['currentOauth']['bd_uid'])) {
                $bd_uid = $userInfo['currentOauth']['bd_uid'];
            }
        }
        if($bd_uid){
            return self::createToken($bd_uid);
        }else{
            return false;
        }
    }

    /**
     * 接口签名验证方法
     *
     * @return object
     */
    public static function checkSign($allParams)
    {
        if (empty($allParams)) {
            return ['code' => 2, 'message' => '非法请求'];
        }

        $isSuccess = true;
        if (empty($allParams) || !isset($allParams['sign'])) {
            $isSuccess = false;
        } else {
            $postSign = strtoupper($allParams['sign']);
            unset($allParams['sign']);
            ksort($allParams);
            $signContent = '';
            foreach ($allParams as $key => $value) {
                if (empty($value)) {
                    continue;
                }
                if (is_array($value)) {
                    $value = json_encode($value, JSON_UNESCAPED_UNICODE);
                }

                $signContent .= $key . '=' . $value . '&';
            }
            $sign = strtoupper(sha1($signContent . self::SITESECRET));
            if ($postSign !== $sign) {
                $isSuccess = false;
            }

        }

        if (!$isSuccess) {
            return ['code' => 2, 'message' => '签名错误'];
        }
        /*if (!isset($allParams['timestamp']) && time() - $allParams['timestamp'] >= 600) {
            return ['code' => 2, 'message' => '请求过期'];
        }*/
    }
}

?>